package com.example.Servlet;

import com.example.Service.UserService;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "deleteUser", value = "/deleteUser")
public class deleteUser extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userIdStr=request.getParameter("userId");
        //2 调用业务逻辑，完成删除功能
       UserService userService=new UserService();
        int rows =userService.deleteUser(Integer.parseInt(userIdStr));
        //3 响应给前端，JSON数据
        Map<String,Object> map=new HashMap<>();
        if(rows>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",500);
            map.put("msg","删除失败");
        }
        Gson gson=new Gson();
        String json=gson.toJson(map);
        response.getWriter().append(json);


    }

    public void destroy() {
    }
}